﻿using System;

namespace Acoris.Models
{
    using System.Globalization;
    using System.Windows.Data;

    public delegate object ConvertHandler(object value, Type targetType, object parameter, CultureInfo culture);

    public class ConverterBase : IValueConverter
    {
        #region Fields

        private ConvertHandler SourceToValue;
        private ConvertHandler ValueToSource;

        #endregion

        #region Constructor

        public ConverterBase(ConvertHandler value2Source) : this(value2Source, null) { }

        public ConverterBase(ConvertHandler value2Source, ConvertHandler source2Value)
        {
            ValueToSource = value2Source ?? throw new NullReferenceException();
            SourceToValue = source2Value;
        }
        #endregion

        #region Method

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return ValueToSource(value, targetType, parameter, culture);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return SourceToValue?.Invoke(value, targetType, parameter, culture);
        }

        #endregion
    }
}
